package com.knowswift.issues.controller;

import com.knowswift.common.bean.message.po.MessageSend;
import com.knowswift.common.bean.message.vo.MessageVO;
import com.knowswift.common.common.BaseResponse;
import com.knowswift.common.common.Page;
import com.knowswift.log.aop.AopLog;
import com.knowswift.security.account.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/app/message/")
public class UserMessageController extends AbstractController {

    @PostMapping("/messages")
    @AopLog(name = "消息列表", platform = "用户端")
    public BaseResponse messages(@AuthenticationPrincipal User user, @RequestBody MessageVO param) {
        messageService.getBaseMapper().read(user.getUserId());
        Page<MessageVO> page = new Page<>(param.getPageNo(), param.getPageSize());
        page = messageService.getBaseMapper().getMessages(user.getUserId(), page);
        return BaseResponse.success(page);
    }

    @PostMapping("/newMessageCount")
    @AopLog(saveLog = false, name = "未读消息数量", platform = "用户端")
    public BaseResponse newMessageCount(@AuthenticationPrincipal User user) {
        int i = messageService.getBaseMapper().getNewMessageCount(user.getUserId());
        return BaseResponse.success(i);
    }

    @PostMapping("/read")
    @AopLog(name = "消息已读", platform = "用户端")
    public BaseResponse read(@AuthenticationPrincipal User user, @RequestBody MessageVO param) {
        boolean update = messageSendService.lambdaUpdate()
                .eq(MessageSend::getUserId, user.getUserId())
                .eq(StringUtils.isNotBlank(param.getMessageSendId()), MessageSend::getMessageSendId, param.getMessageSendId())
                .set(MessageSend::getReadMessage, 1)
                .update();
        return BaseResponse.success();
    }
}
